/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.querie.test;

import com.querie.db.DBMSConnection;
import com.querie.util.QryParameters;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Dictionary;
import java.util.Enumeration;

/**
 *
 * @author Duc On <ductuanon@hotmail.com>
 */
public class ThreadSave extends Thread {

    private Dictionary dict;

    public ThreadSave(Dictionary d) {
        dict = d;
    }

    public void run() {
        Enumeration keys = dict.keys();
        String name = "";
        String value = "";
        String testString = "";
        String trainString = "";
        String[] splitString = null;
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            while (keys.hasMoreElements()) {
                name = (String) keys.nextElement();
                if (name.startsWith("session")) {
                    value = (String) dict.get(name);
                    splitString = name.split("_");

                    if (value.equals("Train")) {
                        trainString = trainString + splitString[1] + ",";
                    } else {
                        testString = testString + splitString[1] + ",";
                    }
                }
            }

            trainString = trainString.substring(0, trainString.length() - 1);
            testString = testString.substring(0, testString.length() - 1);

            String sql1 = "update qryparameters set keyvalue = ? where keyparam= ?";
            con = DBMSConnection.getConnection();
            pstmt = con.prepareStatement(sql1);

            pstmt.setString(1, trainString);
            pstmt.setString(2, "TRAINSESSION");
            pstmt.executeUpdate();

            pstmt.setString(1, testString);
            pstmt.setString(2, "TESTSESSION");

            pstmt.executeUpdate();

            QryParameters qParam = QryParameters.getInstance();
            qParam.setValue("TRAINSESSION", trainString);
            qParam.setValue("TESTSESSION", testString);

            new TestHarnessButtonStatus().enableAllButtons();

        } catch(Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                DBMSConnection.safeClose(rs, pstmt, con);
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
